<template>
  <view class="content">
    <image v-for="(i,idx) in file" @click="delimg(idx)" :key="idx" :src="urlimg+ i.sFileUrl"
      style="width: 100%;display: block;" mode="widthFix"></image>
    <view style="height: 120rpx;"></view>
    <view class="butbox">
      <view class="x-row">
        <view class="x-col btn" @click="choseImg">拍照/上传图片</view>

        <view class="x-col btn" v-if="file.length>0" @click="sureSend">确认提交</view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      file: [],
      urlimg: this.urlimg,
    }
  },
  onLoad() {
    if (getApp().globalData.sureSelectImg) {
      this.file = getApp().globalData.sureSelectImg
    }
  },
  methods: {
    delimg(idx) {
      let f = [],
        that = this
      uni.showModal({
        title: '确认删除该图片吗？',
        success(res) {
          if (res.confirm) {
            for (let i in that.file) {
              if (i != idx) {
                f.push(that.file[i])
              }
            }
            that.file = f
          }
        },
      })
    },
    sureSend() {
      getApp().globalData.sureSelectImg = this.file
      uni.navigateBack({
        delta: 1,
      })
    },
    choseImg() {
      let that = this
      uni.chooseImage({
        count: 9,
        success: (chooseImageRes) => {
          const t = chooseImageRes.tempFilePaths
          for (let i in t) {
            uni.uploadFile({
              url: this.urlimg + 'api/Upfile/PostImgFile',
              filePath: t[i],
              name: 'file',
              header: { token: uni.getStorageSync('token') },
              fileType: 'image',
              success: (data) => {
                let files = JSON.parse(data.data).Value
                that.file.push(files[0])
                uni.showModal({
                  content: '上传成功',
                  showCancel: false,
                })
              },
              complete: () => {},
            })
          }
        },
      })
    },
  },
}
</script>

<style scoped>
.butbox {
  position: fixed;
  bottom: 20upx;
  left: 5%;
  width: 90%;
}

.butbox .btn {
  height: 85upx;
  background: #007aff;
  border-radius: 10upx;
  text-align: center;
  color: #fff;
  font-size: 32upx;
  line-height: 85upx;
  margin: 0 20rpx;
}
</style>
